import axios from "axios";
import {ElMessage} from "element-plus";

axios.defaults.baseURL = "http://localhost:8081"

const doPost = (url, data) => {
    return axios.post(url, data, {headers: {"token": localStorage.getItem("token")}}).then(res => res.data)
}
const doGet = (url, data) => {
    return axios.get(url, {params: data, headers: {"token": localStorage.getItem("token")}}).then(res => {
        if (res.data.code === "401") {
            localStorage.removeItem('token')
            localStorage.removeItem('user')
            // alert("登录失效，请重新登录")
            ElMessage.error('登录失效，请重新登录')
            window.open('http://localhost:8080/#/login')
        }
        return res.data
    })
}

let doThis = {
    doPost,
    doGet
}
export default doThis
